var alldata='';
requetData();
function requetData() {
    $.ajax({
        url:"/gamelibrary/gamelist" ,
        dataType:"json",
        type:"get",
        async:false,
        success:function (data) {
            console.log(data)
            showData(data)
            alldata= data
        }
    })

}

function gameevaluate(gameId) {
    document.location.href="/evaluate.html?gameId="+gameId;
}

function add0(m){return m<10?'0'+m:m }

function format(shijianchuo)
{
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth()+1;
    var d = time.getDate()+1;

    return y+'-'+add0(m)+'-'+add0(d);

}
function  showData(jsonObj) {
    var html = ' <tr><th>游戏截图</th><th>游戏名称</th><th>游戏获取时间</th><th>操作</th><th></th></tr>'

    for(var i = 0; i < jsonObj.length; i ++) {
        var date = format(jsonObj[i].glTime)
        html = html + "<tr ><td class='tdclass'>"
        html = html +"<img src='" + jsonObj[i].gamePicture + "' height=100px width=180px></img>"
        html = html + "</td><td>"
        html = html + jsonObj[i].gameName
        html = html + "</td><td>"
        html = html +  date
        html = html + "</td>"
        html = html + "<td><a download='" + jsonObj[i].gameBody + "' href='" + jsonObj[i].gameBody + "'>下载</a></td>"
        html = html + "<td><button  class='btnv6_blue_hoverfade  btn btn-primary btn-sm' onclick='gameevaluate(" + jsonObj[i].gameId + ")'>评价</button></td></td>"
        html = html + "</tr>"
    }

    $('.table').html(html)
}



function charactersort() {
    var data = alldata
    function compareFunction(a,b){
        return a.gameName.localeCompare(b.gameName,'zh-Hans-CN', {sensitivity: 'accent'})
    }
    data.sort(compareFunction)
    console.log(data)
    showData(data)
}

function  datasort () {
    var data = alldata
    function compareFunction(a,b){
        return b.glTime.localeCompare(a.glTime)
    }
    data.sort(compareFunction)
    console.log(data)
    showData(data)
}
function sortLike() {
    var name = $('input[name="search"]').val()
    $.ajax({
        url:"/gamelibrary/selectgame"  ,
        data: "name=" + name,
        dataType:"json",
        type:"get",
        success:function (data) {
            console.log(data)
            showData(data)
        }
    })
}